package com.youmu.sichuansfc.controller;

import com.youmu.sichuansfc.common.CommonRes;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 *  文件
 */
@Controller
public class FileUploadController {

    private Logger logger = Logger.getLogger(FileUploadController.class);

    //父级文件目录地址
    //private  final  String filePath = "";


    /**
     *  文件长传
     * @param file
     * @return
     */
    @RequestMapping(value = "/fileUpload")
    @ResponseBody
    public CommonRes fileUpload(@RequestParam("fileName") MultipartFile file){
        final  String filePath = "/usr/local/apache-tomcat-8.5.24/webapps/filetest";

        // 获取文件信息
//        String fileName = file.getOriginalFilename();
       int size = (int) file.getSize();
//        String fileType = file.getContentType();

        String name = file.getOriginalFilename() ;
        String fileSuffix  = name.substring(name.lastIndexOf(".")+1,name.length());
        String oldName = name.replaceAll("." + fileSuffix,"");
        oldName = getFileName(filePath,oldName,fileSuffix,0);
        //String fileName = fileSuffix;//new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String newName = oldName + "." + fileSuffix;
        //TODO
        // check file type
//        if(!"application/pdf".equals(fileType) && !"application/zip".equals(fileType)){
//            return  CommonRes.checkFail("请上传PDF文件或压缩包");
//        }
        // check file size
        if (size < 0 || size > 25 * 1024 * 1024){
            return CommonRes.checkFail("文件大于20M");
        }

        // fileName = DateUtil.getNewTime("yyy-MM-dd HH:mm:ss").getTime()+"."+fileType.substring(fileType.lastIndexOf("/")+1);
        File dest = new File(filePath + "/" + newName);

        //判断文件父目录是否存在 否创建
        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdir();
        }
        try {
            //保存文件
            file.transferTo(dest);
            String url = dest.getPath().replaceAll("/usr/local/apache-tomcat-8.5.24/webapps/","http://sc.castingyou.cn/" );
            return CommonRes.success(url);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  CommonRes.fail();
    }

    /**
     * 递归文件名 +1
     * @param path
     * @param name
     * @param suffix
     * @param i
     * @return
     */
    private String getFileName(String path, String name, String suffix, int i) {
        String newname = name+((i>0?"-"+i:""));
        if(new File(path,newname+"."+suffix).exists()) {
            return getFileName(path, name,suffix, i+1);
        }
        return newname;
    }

}
